package rifl.meres4.ui;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.service.log.LogService;
import org.osgi.util.tracker.ServiceTracker;

import rifl.meres4.development.step.IArchitectureStep;
import rifl.meres4.development.step.IImplementationStep;
import rifl.meres4.development.step.IIntegrationStep;
import rifl.meres4.development.step.ISpecificationStep;
import rifl.meres4.development.step.IUIImplementationStep;
import rifl.meres4.release.step.IReleaseStep;
import rifl.meres4.release.step.IRepairStep;
import rifl.meres4.release.step.ITestStep;
import rifl.meres4.ui.tracker.ArchitectureStepServiceTrackerCustomizer;
import rifl.meres4.ui.tracker.ImplementationStepServiceTrackerCustomizer;
import rifl.meres4.ui.tracker.IntegrationStepServiceTrackerCustomizer;
import rifl.meres4.ui.tracker.LogServiceTrackerCustomizer;
import rifl.meres4.ui.tracker.ReleaseStepServiceTrackerCustomizer;
import rifl.meres4.ui.tracker.RepairStepServiceTrackerCustomizer;
import rifl.meres4.ui.tracker.SpecificationStepServiceTrackerCustomizer;
import rifl.meres4.ui.tracker.TestStepServiceTrackerCustomizer;
import rifl.meres4.ui.tracker.UIImplementationStepServiceTrackerCustomizer;

public class Activator implements BundleActivator {

	private static BundleContext context;

	private ServiceTracker<ISpecificationStep, Object> specTracker;
	private ServiceTracker<IArchitectureStep, Object> archTracker;
	private ServiceTracker<IImplementationStep, Object> implementationTracker;
	private ServiceTracker<IUIImplementationStep, Object> uiTracker;
	private ServiceTracker<IIntegrationStep, Object> integrationTracker;
	private ServiceTracker<ITestStep, Object> testTracker;
	private ServiceTracker<IRepairStep, Object> repairTracker;
	private ServiceTracker<IReleaseStep, Object> releaseTracker;
	private ServiceTracker<LogService, Object> logTracker;

	private WorkflowUI workflowUI;
	
	static BundleContext getContext() {
		return context;
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext bundleContext) throws Exception {
		Activator.context = bundleContext;
		
		workflowUI = new WorkflowUI();
		workflowUI.start();
		
		releaseTracker = new ServiceTracker<IReleaseStep, Object>(context, IReleaseStep.class, new ReleaseStepServiceTrackerCustomizer(context, workflowUI));
		releaseTracker.open();
		repairTracker = new ServiceTracker<IRepairStep, Object>(context, IRepairStep.class, new RepairStepServiceTrackerCustomizer(context, workflowUI));
		repairTracker.open();
		testTracker = new ServiceTracker<ITestStep, Object>(context, ITestStep.class, new TestStepServiceTrackerCustomizer(context, workflowUI));
		testTracker.open();
		integrationTracker = new ServiceTracker<IIntegrationStep, Object>(context, IIntegrationStep.class, new IntegrationStepServiceTrackerCustomizer(context, workflowUI));
		integrationTracker.open();
		implementationTracker = new ServiceTracker<IImplementationStep, Object>(context, IImplementationStep.class, new ImplementationStepServiceTrackerCustomizer(context, workflowUI));
		implementationTracker.open();
		uiTracker = new ServiceTracker<IUIImplementationStep, Object>(context, IUIImplementationStep.class, new UIImplementationStepServiceTrackerCustomizer(context, workflowUI));
		uiTracker.open();
		archTracker = new ServiceTracker<IArchitectureStep, Object>(context, IArchitectureStep.class, new ArchitectureStepServiceTrackerCustomizer(context, workflowUI));
		archTracker.open();
		specTracker = new ServiceTracker<ISpecificationStep, Object>(context, ISpecificationStep.class, new SpecificationStepServiceTrackerCustomizer(context, workflowUI));
		specTracker.open();
		
		logTracker = new ServiceTracker<LogService, Object>(context, LogService.class, new LogServiceTrackerCustomizer(context, workflowUI));
		logTracker.open();
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext bundleContext) throws Exception {
		Activator.context = null;
		
		if (this.workflowUI != null) {
			this.workflowUI.stop();
		}
		
		specTracker.close();
		archTracker.close();
		implementationTracker.close();
		uiTracker.close();
		integrationTracker.close();
		logTracker.close();
		releaseTracker.close();
		repairTracker.close();
		testTracker.close();
		logTracker.close();
	}

}
